package com.lczyfz.istep.modules.openstack.api.nova.impl;

import com.lczyfz.istep.modules.openstack.api.nova.impl.parseJson.NovaParse;
import com.lczyfz.istep.modules.openstack.api.nova.interfaces.Hypervisors;
import com.lczyfz.istep.modules.openstack.api.nova.model.Hypervisor;
import com.lczyfz.istep.modules.openstack.constvary.ConstParams;
import com.lczyfz.istep.modules.openstack.constvary.RequestType;
import com.lczyfz.istep.modules.openstack.utils.HTTP;
import com.lczyfz.istep.modules.openstack.utils.Request;
import com.lczyfz.istep.modules.openstack.utils.RequestFactory;
import com.lczyfz.istep.modules.openstack.utils.Response;
import net.sf.json.JSONObject;

import java.util.ArrayList;
/**
 * 管理程序接口的实现类
 * @author clq
 *
 */
public class HypervisorImpl implements Hypervisors{

	public RequestFactory factory = new RequestFactory("nova");
	@Override
	public ArrayList<Hypervisor> getHypervisors() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<Hypervisor> getHypervisorsDetail() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Hypervisor getHypervisorStatistics() {

		Hypervisor hypervisor = new Hypervisor();
		Request request = factory.getRequest();
		request.setUrl("/os-hypervisors/statistics" );
		request.setRequestType(RequestType.GET);

		Response results = HTTP.getConnectionResult(request);
		JSONObject body = results.getResponseBody();
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		hypervisor = NovaParse.resolveHypervisor(body);

		// TODO Auto-generated method stub
		return hypervisor;
	}

	@Override
	public Hypervisor getHypervisorDetail(String HypervisorId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Hypervisor showHypervisorUptime(String HypervisorId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<Hypervisor> getHypervisorServers(String HypervisorId) {
		// TODO Auto-generated method stub
		return null;
	}

	public static void main(String[] args){
		HypervisorImpl hypervisordemo = new HypervisorImpl();
		Hypervisor summary_hypervisor = hypervisordemo.getHypervisorStatistics();
		System.out.println("任务数="+summary_hypervisor.getCurrentWorkload()+" "+"实际可用磁盘="+summary_hypervisor.getDiskAvailableLeast()+" "+
				"剩余的可用磁盘="+summary_hypervisor.getFreeDiskGb()+" "+"空闲RAM="+summary_hypervisor.getFreeRamMb()+" "+"磁盘="+summary_hypervisor.getLocalGb()+
				" "+"已使用的磁盘="+summary_hypervisor.getLocalGbUsed()+" "+"内存="+summary_hypervisor.getMemoryMb()+" "+"已使用的内存="+summary_hypervisor.getMemoryMbUsed()+
				" "+"vcpu数="+summary_hypervisor.getVcpus()+" "+"已使用的vcpu的数量="+summary_hypervisor.getVcpusUsed()+" "+"运行的vms数="+summary_hypervisor.getRunningVms()+" "+"虚拟机管理程序的数量="+summary_hypervisor.getHypervisorCount());
	}
}

